package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 调动分组流程归档action
 * @author sy
 */
@Slf4j
public class AdjustPersonGroupArchivingAction implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("**********AdjustPersonGroupArchivingAction start**********");
        String requestid = requestInfo.getRequestid();
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);

        //调动人员
        String ddry = Util.null2String(mainTableData.get("ddry"));
        //调入日期
        String adjustDate = Util.null2String(mainTableData.get("drrq"));
        //调入分组
        String adjustToGroup = Util.null2String(mainTableData.get("drfz"));
        //调出分组
        String adjustFromGroup = Util.null2String(mainTableData.get("dcfz"));
        //调回日期
        String adjustBackDate = Util.null2String(mainTableData.get("dhrq"));
        //调动说明
        String ddsm = Util.null2String(mainTableData.get("ddsm"));

        try {
            String sql = "";
            if (!"".equals(adjustFromGroup)) {
                sql = "select id, bdate, edate from uf_ryqz_dt1 where mainid = " + adjustFromGroup + " and empid = " + ddry + " and delete_type = 0";
                List<Map<String, Object>> data = DbTools.getSqlToList(sql);
                data = data.stream().
                        filter(f -> Util.null2String(f.get("bdate")).compareTo(adjustDate) < 0
                                && ("".equals(Util.null2String(f.get("edate"))) || Util.null2String(f.get("edate")).compareTo(adjustDate) >= 0))
                        .collect(Collectors.toList());
                if (data.size() > 2) {
                    log.error("更新人员分组数据失败: 调出分组中目标数据不唯一！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 调出分组中目标数据不唯一！");
                    return Action.FAILURE_AND_CONTINUE;
                } else if (data.size() == 1) {
                    //修改调出分组中目标数据的失效日期为调入日期前一天
                    sql = "update uf_ryqz_dt1 set edate = '" + DateUtil.beforeDay(adjustDate, 1) + "' where id = " + Util.null2String(data.get(0).get("id"));
                    boolean updateAdjustFromGroupSign = DbTools.update(sql);
                    if (!updateAdjustFromGroupSign) {
                        log.error("更新人员分组数据失败: 修改调出分组中目标数据的失效日期为调入日期前一天未成功！");
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 修改调出分组中目标数据的失效日期为调入日期前一天未成功！");
                        return Action.FAILURE_AND_CONTINUE;
                    }
                    //新增一条数据在调入分组中。生效日期为调入日期，失效日期为调回日期前一天
                    sql = "insert into uf_ryqz_dt1 (mainid, empid, bdate, edate, create_time, update_time, creator, delete_type) values (?,?,?,?,?,?,?,?)";
                    boolean insertAdjustToGroupSign = DbTools.update(sql, adjustToGroup, ddry, adjustDate,
                            "".equals(adjustBackDate) ? null : DateUtil.beforeDay(adjustBackDate, 1),
                            DateUtil.getCurrentDate(), DateUtil.getCurrentDate(), "1", "0");
                    if (!insertAdjustToGroupSign) {
                        log.error("更新人员分组数据失败: 新增一条数据在调入分组未成功！");
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 新增一条数据在调入分组未成功！");
                        return Action.FAILURE_AND_CONTINUE;
                    }
                    //新增一条数据在调出分组中。生效日期为调回日期
                    if (!"".equals(adjustBackDate)) {
                        sql = "insert into uf_ryqz_dt1 (mainid, empid, bdate, create_time, update_time, creator, delete_type) values (?,?,?,?,?,?,?)";
                        boolean insertBackAdjustFromGroupSign = DbTools.update(sql, adjustFromGroup, ddry, adjustBackDate,
                                DateUtil.getCurrentDate(), DateUtil.getCurrentDate(), "1", "0");
                        if (!insertBackAdjustFromGroupSign) {
                            log.error("更新人员分组数据失败: 新增一条数据在调出分组中（生效日期为调回日期）未成功！");
                            requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                            requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 新增一条数据在调出分组中（生效日期为调回日期）未成功！");
                            return Action.FAILURE_AND_CONTINUE;
                        }
                    }
                } else if (data.size() == 0) {
                    log.error("更新人员分组数据失败: 调出分组中目标数据不存在！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 调出分组中目标数据不存在！");
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                //新增一条数据在调入分组中。生效日期为调入日期，失效日期为调回日期前一天
                sql = "insert into uf_ryqz_dt1 (mainid, empid, bdate, edate, create_time, update_time, creator, delete_type) values (?,?,?,?,?,?,?,?)";
                boolean insertAdjustToGroupSign = DbTools.update(sql, adjustToGroup, ddry, adjustDate,
                        "".equals(adjustBackDate) ? null : DateUtil.beforeDay(adjustBackDate, 1),
                        DateUtil.getCurrentDate(), DateUtil.getCurrentDate(), "1", "0");
                if (!insertAdjustToGroupSign) {
                    log.error("更新人员分组数据失败: 新增一条数据在调入分组未成功！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 新增一条数据在调入分组未成功！");
                    return Action.FAILURE_AND_CONTINUE;
                }
            }


        }catch (Exception e){
            log.error("AdjustPersonGroupArchivingAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
